<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Name:  Setup Model
*
* Author:  William Bermudo
*
*/

class Setup_m extends DataMapperExt {
	
	var $table = 'settings_autogen';
	
	public function __construct()
	{
		parent::__construct();
	}
	
	public function get_numbering()
	{
		$numbering = new Setup_m();
		
		$numbering->where('autogen_inactive', '1');
		$numbering->get();
		
		if ($numbering->result_count() > 0)
		{
			return $numbering;
		}
	}
	
	public function update_numbering($data = array())
	{
		$data = (object)$data;
		
		for ( $i = 0, $count = count($data->type); $i < $count; $i++ )
		{
			$this->db->set('autogen_prefix',$data->prefix[$i]);
			$this->db->set('autogen_suffix',$data->suffix[$i]);
			$this->db->set('autogen_min_digits',$data->min_digit[$i]);
			$this->db->set('autogen_initial_number',$data->init_number[$i]);
			$this->db->set('autogen_current_number',$data->current_number[$i]);
			
			$this->db->where('autogen_type',$data->type[$i]);
			
			$this->db->update('settings_autogen');
		}
	}
	
	public function generate_invoice()
	{
		$numbering = new Setup_m();
		
		$numbering
				->where('autogen_type','transaction')
				->where('autogen_inactive','1')
				->get(1);
		
		if ( $numbering->result_count() > 0 )
		{
			$autogen_num = $this->current_temp_autogen($numbering->id);
			
			if ( ! $autogen_num )
			{
				$autogen_num = $autogen_num + 1;
				
				if ( $autogen_num <  $numbering->autogen_current_number)
				{
					if ( $numbering->autogen_current_number > $numbering->autogen_initial_number ) {
						$autogen_num = $numbering->autogen_current_number;
					} else {
						$autogen_num = $numbering->autogen_initial_number;
					}
				}
			}
			else
			{
				$autogen_num = $autogen_num + 1;
			}
			
			$formatted_num = $autogen_num;
			
			if ( strlen($autogen_num) < $numbering->autogen_min_digits )
			{
				$formatted_num = "";
				
				for ( $i = 0; $i < strlen($autogen_num) - $numbering->autogen_min_digits; $i++ )
				{
					$formatted_num = '0'.$formatted_num;
				}
				
			}
			
			$invoice_number = $numbering->autogen_prefix.$formatted_num.$numbering->autogen_suffix;
			
			$data = array(
				'autogen_seq_id'	=> $numbering->id,
				'tmp_invoice_number'=> $invoice_number,
				'tmp_current_number'=> $autogen_num
			);
			
			$this->insert_temp_autogen( $data );
			
			return $invoice_number;
		}
		else
		{
			return 'asd';
		}
	}
	
	public function insert_temp_autogen( $data )
	{
		$this->db->insert('temp_autogen',$data);
	}
	
	public function current_temp_autogen( $id )
	{
		$this->db->select("MAX( `tmp_current_number` ) AS 'current_num' ",FALSE);
		
		$this->db->where('autogen_seq_id',$id);
		
		$result = $this->db->get('temp_autogen');
		
		$row = $result->row();
		
		if ( $row->current_num )
		{
			return $row->current_num;
		}
		else
		{
			return false;
		}
	}
	
}